package com.yj.springbootmybatis.service;

import com.yj.springbootmybatis.mapper.UserAddressMapper;
import com.yj.springbootmybatis.pojo.bo.UserAddressBO;
import com.yj.springbootmybatis.pojo.vo.UserAddressVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    UserAddressMapper userAddressMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserAddressVO> queryAll(String userId) {

        UserAddressVO userAddressVO = new UserAddressVO();
        userAddressVO.setUserId(userId);

        List<UserAddressVO> list = userAddressMapper.select(userAddressVO);

        return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void addNewUserAddress(UserAddressBO userAddressBO) {
        int isDefault = 0;
        List<UserAddressVO> list = queryAll(userAddressBO.getUserId());
        if (list == null ||list.isEmpty() ||list.size() == 0){
            isDefault = 1;
        }
        UserAddressVO newUserAddress = new UserAddressVO();
        BeanUtils.copyProperties(userAddressBO,newUserAddress);
        newUserAddress.setIsDefault(isDefault);
        newUserAddress.setCreateTime(new Date());
        newUserAddress.setUpdateTime(new Date());
        userAddressMapper.insert(newUserAddress);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int updateUserAddress(UserAddressBO userAddressBO) {

        UserAddressVO userAddressVO = new UserAddressVO();
        BeanUtils.copyProperties(userAddressBO,userAddressVO);
        userAddressVO.setId(userAddressBO.getId());
        userAddressVO.setUpdateTime(new Date());
        log.info("UserAddressVO:{}",userAddressVO);
        int result = userAddressMapper.updateByPrimaryKeySelective(userAddressVO);

        return result;
    }

    @Override
    public int deleteUserAddress(UserAddressBO userAddressBO) {

        UserAddressVO userAddressVO = new UserAddressVO();
        userAddressVO.setId(userAddressBO.getId());
        userAddressVO.setUserId(userAddressBO.getUserId());
        int result = userAddressMapper.delete(userAddressVO);

        return result;
    }

    @Override
    public void setDefaultUserAddress(UserAddressBO userAddressBO) {

        //1.先将原来设置为默认的地址修改为不默认
        UserAddressVO dbUserAddressVO = new UserAddressVO();
        dbUserAddressVO.setUserId(userAddressBO.getUserId());
        dbUserAddressVO.setIsDefault(1);
        List<UserAddressVO> list = userAddressMapper.select(dbUserAddressVO);

        for (UserAddressVO userAddressVO : list) {
            userAddressVO.setIsDefault(0);
            userAddressMapper.updateByPrimaryKeySelective(userAddressVO);
        }
        //2.然后将当前地址修改为默认
        UserAddressVO defaultAddressVO = new UserAddressVO();
        defaultAddressVO.setId(userAddressBO.getId());
        defaultAddressVO.setUserId(userAddressBO.getUserId());
        defaultAddressVO.setIsDefault(1);

        userAddressMapper.updateByPrimaryKeySelective(defaultAddressVO);



    }
}
